package com.weapons.week0903_weapons.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.bean.RecommentBean;

import java.util.List;

/**
 * A custom adapter to use with the RecyclerView widget.
 */
public class RoundRecycleAdapter extends RecyclerView.Adapter<RoundRecycleAdapter.ViewHolder>{
    private List<RecommentBean.ItemBean> mDatas;
    private String TAG= "pos";
    private Context mContext;
    public RoundRecycleAdapter(List<RecommentBean.ItemBean> mDatas,Context context){
        this.mDatas=mDatas;
        this.mContext=context;
        Log.i("pos","RoundRecycleAdapter"+mDatas.size());
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_child, viewGroup, false);

        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {


        holder.tv_child_title.setText(mDatas.get(position).getTitle());
        holder.tv_time.setText(mDatas.get(position).getPubDate());
        holder.tv_count.setText(mDatas.get(position).getComments()+"评论");
        Picasso.with(mContext).load(mDatas.get(position).getImage())
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .into(holder.iv_child);

        if (mListener != null)
            holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(position, holder.itemView);
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.i("pos","mDatasssssss"+mDatas.size());
        return mDatas==null?0:mDatas.size();
    }



    public interface OnItemClickListener {
        void onItemClick(int pos, View itemView);
    }
    //定义一个接口的成员变量
    private OnItemClickListener mListener;
    //暴露出公共方法，把接口传过来，给成员变量赋值
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_child;
        private TextView tv_child_title,tv_count,tv_time;
        public ViewHolder(View itemView) {
            super(itemView);
            iv_child= (ImageView) itemView.findViewById(R.id.iv_child);
            Log.i("pos","到这了吗");
            tv_child_title= (TextView) itemView.findViewById(R.id.tv_child_title);
            tv_time= (TextView) itemView.findViewById(R.id.tv_time);
            tv_count= (TextView) itemView.findViewById(R.id.tv_count);
        }
    }
}
